package ar.com.photo_admin.service

import ar.com.photo_admin.domain.*;
import org.grails.plugins.google.visualization.util.DateUtil;
import org.grails.plugins.google.visualization.data.Cell
import groovy.time.TimeCategory;

class StatisticsService extends BaseService {

    def generateAmounOrdersRecievedInLastYearData() {		
		def ordersInLastYear = getLastYearOrders()
		
		Calendar cal = Calendar.instance
		def data = []					
		for (int i = 0 ; i <12 ; i++){
			Date intervalEnd = cal.getTime()
			cal.add(Calendar.MONTH, -1)
			Date intervalBeginning = cal.getTime()
			def ordersInterval = ordersInLastYear.findAll{ it.dateCreated > intervalBeginning && it.dateCreated < intervalEnd }
			data << [DateUtil.createDate(intervalEnd.year+1900, intervalEnd.month, intervalEnd.day) , 
						new Cell(value: ordersInterval.size()),
						new Cell(value: ordersInterval.count { it.paidOnline })]			
		}				
		return data		
    }
	
	def generateIncomeInLastYearData() {
		def ordersInLastYear = getLastYearOrders()
		
		Calendar cal = Calendar.instance
		def data = []
			
		for (int i = 0 ; i <12 ; i++){
			Date intervalEnd = cal.getTime()
			cal.add(Calendar.MONTH, -1)
			Date intervalBeginning = cal.getTime()
			def ordersInterval = ordersInLastYear.findAll{ it.dateCreated > intervalBeginning && it.dateCreated < intervalEnd }
			
			def incomeTotal = 0
			def incomeOnline = 0
			ordersInterval.each { order ->
				incomeOnline += order.paidOnline ? order.price : 0
				incomeTotal += order.price
			}
			
			data << [DateUtil.createDate(intervalEnd.year+1900, intervalEnd.month, intervalEnd.day) ,
						new Cell(value: incomeTotal),
						new Cell(value: incomeOnline)]
		}
				
		return data
	}
	
	def getTotalAmountOrders(){
		Photographer loggedPhotographer = getLoggedUser()
		return PurchaseOrder.countByPhotographer(loggedPhotographer);
	}
	
	def getTotalAmountEvents(){
		Photographer loggedPhotographer = getLoggedUser()
		return Event.countByPhotographer(loggedPhotographer);
	}
	
	def getTotalIncomeOrders(){		
		def orders = getAllPhotographersOrders()
		def incomeTotal = 0		
		orders.each { order ->			
			incomeTotal += order.price
		}
		return Math.round(incomeTotal);
	}
	
	def getAmountNotFinalized(){
		def orders = getAllPhotographersOrders()				
		return orders.count { PurchaseOrder order -> order.isFinalizable() } 
		
	}
	
	def getAllPhotographersOrders(){
		Photographer loggedPhotographer = getLoggedUser()
		return PurchaseOrder.findAllByPhotographer(loggedPhotographer);
	}
	
	def getLastYearOrders(){
		Photographer loggedPhotographer = getLoggedUser()
		def now = new Date()
		def intervalOrders =PurchaseOrder.findAllByPhotographerAndDateCreatedBetween(loggedPhotographer,new Date()-365,new Date()+1) 
		return intervalOrders
	}
	
	def  generateAmountEventsAndAlbumsInLastYearData(){

		//Busco los eventos que se visualizaran		
		Photographer loggedPhotographer = getLoggedUser()
		def eventsLastYear = Event.findAllByPhotographerAndDateCreatedBetween(loggedPhotographer,new Date()-365,new Date()+1)
		
		//Genero la data por mes en formato correspodneitne
		Calendar cal = Calendar.instance
		def data = []
		for (int i = 0 ; i <12 ; i++){
			Date intervalEnd = cal.getTime()
			cal.add(Calendar.MONTH, -1)
			Date intervalBeginning = cal.getTime()
			def eventsInterval = eventsLastYear.findAll{ it.eventDate > intervalBeginning && it.eventDate < intervalEnd }
			
			def albumsTotal = 0
			eventsInterval.each { ev ->
				albumsTotal += ev.albums.size()
			}
			

			data << [DateUtil.createDate(intervalEnd.year+1900, intervalEnd.month, intervalEnd.day) ,
						new Cell(value: eventsInterval.size()),
						new Cell(value: albumsTotal)]
		}
		return data
	}
	
}
